﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SCSService
{
    public class InvoiceService
    {
        #region Customer_Order
        public List<Customer_Order> GetListOrder(int? status,DateTime? startDate,DateTime? endDate,int? siteGuid)
        {
            using(var data =new DataDataContext() )
            {
               
                var result = (from p in data.Customer_Orders
                              where p.SiteGuid==siteGuid
                              select p);
                              
                if (startDate != null && endDate !=null)
                {
                    result = result.Where(p => p.CreateDate >= startDate && p.CreateDate <= endDate);
                }

                if (startDate != null && endDate == null)
                {
                    result = result.Where(p => p.CreateDate >= startDate);
                }

                if (startDate == null && endDate != null)
                {
                    result = result.Where(p => p.CreateDate <= endDate);
                }

                if (status != null)
                {
                    result = result.Where(p => p.OrderStatus == (byte)status);

                }

                return result.ToList();
            }
             
        }

        public Customer_Order GetOrderByID(int id)
        {
            using (var data = new DataDataContext())
            {
                var cus = (from p in data.Customer_Orders
                           where p.OrderID == id
                           select p);
                return cus.FirstOrDefault();

            }

        }

        public Invoice GetInvoiveByID(int id)
        {
            using (var data = new DataDataContext())
            {
                var invoices = (from p in data.Invoices
                                where p.InvoiceID == id
                                select p);
                return invoices.FirstOrDefault();
            }
        }
        #endregion
        #region Customer Order Detail
   
        public int GetCountOrderItems(int typeId)
        {
            using (var data = new DataDataContext())
            {
                var OrderItemMana = (from p in data.Order_Items
                               where p.OrderID == typeId
                               select p);
                return OrderItemMana.Count();
            }
        }
      
        public void DeleteOrderItems(int? typeId)
        { 
            using(var data=new DataDataContext())
            {
                var OrderItems = (from p in data.Order_Items
                                  where p.OrderID == typeId
                                  select p).FirstOrDefault();
                data.Order_Items.DeleteOnSubmit(OrderItems);
                data.SubmitChanges();
            }
        }

        public void DeleteOrder_Item_Detail(int? typeId)
        {
            using (var data = new DataDataContext())
            {
                var OrderDetailItems = (from p in data.Order_Item_Details
                                  where p.OrderItemDetailID == typeId
                                  select p).FirstOrDefault();
                data.Order_Item_Details.DeleteOnSubmit(OrderDetailItems);
                data.SubmitChanges();
            }
        }
       
        public int GetCountProduct_Stock_Detail(int? typeId)
        {
            using (var data = new DataDataContext())
            {
                var Product_Stock_DetailMana = (from p in data.Product_Stock_Details
                                     where p.ProductStockDetailID == typeId
                                     select p);
                return Product_Stock_DetailMana.Count();
            }
        }
      
        public void UpdateStatusProduct_Stock_Detail(int? typeId,byte? status)
        {
            using (var data = new DataDataContext())
            {
                var GetProduct_Stock_Detail = (from p in data.Product_Stock_Details
                                  where p.ProductStockDetailID == typeId
                                  select p).FirstOrDefault();
                GetProduct_Stock_Detail.Status= status;
                data.SubmitChanges();
            }
        }
       

        public List<Detail_Order> CustomerOrderDetail(int? id)
        {
            using (var data = new DataDataContext())
            {

                List<Detail_Order> result = (from p in data.Customer_Orders
                              join ot in data.Order_Items on p.OrderID equals ot.OrderID into r0
                              from e0 in r0
                              join oid in data.Order_Item_Details on e0.OrderItemID equals oid.OrderItemID into r2
                              from e1 in r2.DefaultIfEmpty()
                              join psd in data.Product_Stock_Details on e1.ProductStockDetailID equals psd.ProductStockDetailID into r3
                              from e2 in r3.DefaultIfEmpty()
                                             where p.OrderID==id
                              select new Detail_Order
                              {
                                  OrderID = p.OrderID,
                                  CreateDate = p.CreateDate,
                                  FullName = p.Fullname,
                                  StatusOrder = p.OrderStatus,
                                  Total = p.Total,
                                  Email = p.Email,
                                  Phone = p.Phone,
                                  ProductID = e0.ProductID,
                                  OrderItemID = e0.OrderItemID,
                                  Code = e2.Code,
                                  Serial = e2.Serial,
                                  ProductStockDetailID=e2.ProductStockDetailID,
                                  OrderItemDetailID = e1.OrderItemDetailID
                              }).ToList();
                return result;
            }
            
        }

        public void DeleteCustomerOrder(int? typeId)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Customer_Orders
                            where p.OrderID == typeId
                            select p).FirstOrDefault();
                data.Customer_Orders.DeleteOnSubmit(type);
                data.SubmitChanges();
            }
        }
        #endregion
        public void DeleteInvoice(int? typeId, byte? status)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Invoices
                            where p.OrderID == typeId && p.Status==status
                            select p).FirstOrDefault();
                data.Invoices.DeleteOnSubmit(type);
                data.SubmitChanges();
            }
        }
        public string GetProductNameByID(int? typeId)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Products
                            where p.ProductID == typeId
                            select p).FirstOrDefault();
                return type.ProductName;
            }

        }
        #region Shipment
        public List<Shipment> GetListShipment(int? typeId)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Shipments
                            where p.OrderID == typeId
                            select p).ToList();
                return type;
            }
        }
        public Shipment GetShipmentByOrderID(int id)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Shipments
                            where p.OrderID == id
                            select p).FirstOrDefault();
                return type;
            }
        }
        public int GetStatusShipmnetByShipment(int OrderItemID)
        {
            int GetShipmentID;
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Shipment_Items
                            where p.OrderItemID == OrderItemID
                            select p.ShipmentID).FirstOrDefault();
                GetShipmentID =type;

                var GetStatus = (from p in data.Shipments
                            where p.ShipmentID == GetShipmentID
                            select p.Status).FirstOrDefault();
                return GetStatus ?? -1;
            }

           
               
            

        }
        public void InsertShipment_Item(Shipment_Item prtShipmentItem)
        { 
            using(var data=new DataDataContext())
            {
                data.Shipment_Items.InsertOnSubmit(prtShipmentItem);
                data.SubmitChanges();
            }
        }
        #endregion 
        #region Order_items
        public Order_Item GetOrder_ItemByID(int OrderItemID)
        { 
          using(var data=new DataDataContext())
          {
              var type = (from OrItem in data.Order_Items
                          where OrItem.OrderItemID == OrderItemID
                          select OrItem).FirstOrDefault();

              return type;
          }
            
        }
        #endregion
        #region Shipment Items
        public void DeleteShipment_Item(int ShipmentID, int OrderItemID)
        { 
            using(var data=new DataDataContext())
            {
                var type = (from shipmentItem in data.Shipment_Items
                            where shipmentItem.ShipmentID == ShipmentID && shipmentItem.OrderItemID == OrderItemID
                            select shipmentItem).FirstOrDefault();
                data.Shipment_Items.DeleteOnSubmit(type);
                data.SubmitChanges();
            }

        }
        #endregion

        #region Báo cáo thống kê.
          
        #endregion
    }
}
